﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.IO;
using System.Collections.Specialized;
using Guru.Web.UI;
using Guru.Utilities;

namespace Guru.Web.Controls.Asp
{
    public class TextBox : System.Web.UI.WebControls.TextBox, IMultiLanguageLoader
    {
        #region Constants
        protected const string KEY_HTML_TEMP_TEXTAREA = "Guru.Web.Controls.Asp.TextBoxArea";
        protected const string KEY_HTML_TEMP_NORMAL = "Guru.Web.Controls.Asp.TextBox";
        protected const string JS_KEY_PRESS = "return Guru_ASPNet_CatchEnterOnTextBox(event, '{0}');";
        #endregion       
        private WebControl _AcceptButtonObject;
      
        public TextBox(): base()
        {
            
        }

        protected WebControl AcceptButtonObject
        {
            get
            {
                if (string.IsNullOrEmpty(this.AcceptButtonID))
                {
                    return null;
                }
                else if (_AcceptButtonObject != null)
                {
                    return _AcceptButtonObject;
                }
                else
                {
                    // Find in current container
                    _AcceptButtonObject = ControlExtensions.FindOuterControl(this.Parent, this.AcceptButtonID) as WebControl;
                    return _AcceptButtonObject;
                }
            }
        }
        public bool IsStandard
        {
            get;
            set;
        }
        public string AcceptButtonID
        {
            get
            {
                return ViewState["AcceptButton"] as string;
            }
            set
            {
                ViewState["AcceptButton"] = value;
                _AcceptButtonObject = null;
            }
        }        

        #region Override
        public override string Text
        {
            get
            {
                string val= base.Text;
                if (this.MaxLength > 0 && val.Length > this.MaxLength)
                {
                    return string.Empty;
                }
                return val;
            }
            set
            {
                base.Text = value;
            }
        }
        public void SetText(NameValueCollection text)
        {
            this.ToolTip = text[this.ID + ".Tooltip"];
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.LoadTextFromResource(((GuruPage)this.Page).CurrentLanguage);            
        }       
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            //Regist javascript
            string clientScript = Page.ClientScript.GetWebResourceUrl(typeof(Guru.Web.Controls.Asp.TextBox), "Guru.Web.Controls.EmbeddedResource.ASPNetTextBox.js");
            Page.ClientScript.RegisterClientScriptInclude("Guru.Web.Controls.EmbeddedResource.ASPNetTextBox.js", clientScript);
            string keyPressEvent = string.Empty;
            if (this.AcceptButtonObject != null)
            {
                keyPressEvent = string.Format(JS_KEY_PRESS, this.AcceptButtonObject.ClientID);
            }
            else if (this.TextMode != TextBoxMode.MultiLine)
            {
                keyPressEvent = string.Format(JS_KEY_PRESS, string.Empty);
            }
            this.Attributes.Add("onkeypress", keyPressEvent);
        }
        
        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            if (this.IsStandard)
            {
                base.Render(writer);
            }
            else
            {
                string key = (this.TextMode == TextBoxMode.MultiLine) ? KEY_HTML_TEMP_TEXTAREA : KEY_HTML_TEMP_NORMAL;
                if (this.Width.Type == UnitType.Percentage) key += ".Percentage";
                string template = HTMLTemplate.GetByKey(key);
                StringBuilder baseHTML = new StringBuilder();
               
                using (StringWriter twr = new StringWriter(baseHTML))
                {
                    using (HtmlTextWriter newWriter = new HtmlTextWriter(twr))
                    {
                        base.Render(newWriter);
                        newWriter.Flush();
                        writer.Write(string.Format(template, this.ClientID, baseHTML, this.Width.Value));
                    }
                }
            }
        }
        #endregion
    }
}
